home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 5 / Amiga Plus Sonderheft 1996 #5.iso / programme / picturedt_v43 / demo / pbm / ffr.c next >
C/C++ Source or Header  |  1986-04-25  |  3KB  |  180 lines

  1.  
  2. #include "PBM.h"
  3.  
  4. #include <exec/memory.h>
  5.  
  6. #include <string.h>
  7.  
  8. #define BUFFER_SIZE 32768L
  9. #define FIRST_BLOCK 512L
  10.  
  11. struct FFRHandle
  12.  {
  13.   struct StandardPacket fh_Pkt;
  14.   struct MsgPort *fh_ReplyPort;
  15.   struct FileHandle *fh_Handle;
  16.   BOOL fh_Flag;
  17.   UBYTE *fh_Buffer,*fh_Pre,*fh_Ptr;
  18.   ULONG fh_Left;
  19.  };
  20.  
  21. void __regargs BeginRead(struct FFRHandle *FH,LONG Bytes)
  22.  
  23. {
  24.  struct StandardPacket *SP;
  25.  
  26.  SP=&FH->fh_Pkt;
  27.  
  28.  SP->sp_Msg.mn_Node.ln_Name=(char *)&SP->sp_Pkt;
  29.  SP->sp_Msg.mn_ReplyPort=FH->fh_ReplyPort;
  30.  SP->sp_Msg.mn_Length=sizeof(struct StandardPacket);
  31.  
  32.  SP->sp_Pkt.dp_Link=&SP->sp_Msg;
  33.  SP->sp_Pkt.dp_Port=FH->fh_ReplyPort;
  34.  
  35.  SP->sp_Pkt.dp_Type=ACTION_READ;
  36.  SP->sp_Pkt.dp_Arg1=FH->fh_Handle->fh_Arg1;
  37.  SP->sp_Pkt.dp_Arg2=(LONG)FH->fh_Pre;
  38.  SP->sp_Pkt.dp_Arg3=Bytes;
  39.  
  40.  PutMsg (FH->fh_Handle->fh_Type,&SP->sp_Msg);
  41. }
  42.  
  43. LONG __regargs EndRead(struct FFRHandle *FH)
  44.  
  45. {
  46.  struct StandardPacket *SP;
  47.  
  48.  SP=&FH->fh_Pkt;
  49.  
  50.  (void)WaitPort(FH->fh_ReplyPort);
  51.  (void)GetMsg(FH->fh_ReplyPort);
  52.  
  53.  if (SP->sp_Pkt.dp_Res1<0L)
  54.   {
  55.    MySetIoErr (SP->sp_Pkt.dp_Res2);
  56.    return FALSE;
  57.   }
  58.  
  59.  FH->fh_Ptr=FH->fh_Pre;
  60.  FH->fh_Pre=FH->fh_Buffer;
  61.  FH->fh_Buffer=FH->fh_Ptr;
  62.  FH->fh_Left=SP->sp_Pkt.dp_Res1;
  63.  
  64.  if (SP->sp_Pkt.dp_Res1==SP->sp_Pkt.dp_Arg3) BeginRead (FH,BUFFER_SIZE);
  65.  else FH->fh_Flag=FALSE;
  66.  
  67.  return TRUE;
  68. }
  69.  
  70. void __regargs CloseFFR(struct FFRHandle *FH)
  71.  
  72. {
  73.  if (FH->fh_Flag)
  74.   {
  75.    (void)WaitPort(FH->fh_ReplyPort);
  76.    (void)GetMsg(FH->fh_ReplyPort);
  77.   }
  78.  
  79.  FreeVec (FH->fh_Pre);
  80.  FreeVec (FH->fh_Buffer);
  81.  DeleteMsgPort (FH->fh_ReplyPort);
  82.  FreeVec (FH);
  83. }
  84.  
  85. struct FFRHandle __regargs *OpenFFR(BPTR Handle,LONG PreRead)
  86.  
  87. {
  88.  struct FFRHandle *FH;
  89.  
  90.  if (Handle==NULL) return NULL;
  91.  
  92.  if (FH=AllocVec(sizeof(struct FFRHandle),MEMF_PUBLIC|MEMF_CLEAR))
  93.   {
  94.    if (FH->fh_ReplyPort=CreateMsgPort())
  95.     {
  96.      if (FH->fh_Buffer=AllocVec(BUFFER_SIZE,MEMF_PUBLIC|MEMF_CLEAR))
  97.       {
  98.        if (FH->fh_Pre=AllocVec(BUFFER_SIZE,MEMF_PUBLIC|MEMF_CLEAR))
  99.         {
  100.          FH->fh_Handle=(struct FileHandle *)BADDR(Handle);
  101.          FH->fh_Flag=TRUE;
  102.  
  103.          BeginRead (FH,FIRST_BLOCK-PreRead);
  104.  
  105.          return FH;
  106.         }
  107.        FreeVec (FH->fh_Buffer);
  108.       }
  109.      DeleteMsgPort (FH->fh_ReplyPort);
  110.     }
  111.    FreeVec (FH);
  112.   }
  113.  
  114.  MySetIoErr (ERROR_NO_FREE_STORE);
  115.  return NULL;
  116. }
  117.  
  118. LONG __regargs FFRGetC(struct FFRHandle *FH)
  119.  
  120. {
  121.  if (FH->fh_Left)
  122.   {
  123.    FH->fh_Left--;
  124.    return *FH->fh_Ptr++;
  125.   }
  126.  
  127.  if (!FH->fh_Flag) MySetIoErr (0L);
  128.  else
  129.   if (EndRead(FH))
  130.    if (FH->fh_Left)
  131.     {
  132.      FH->fh_Left--;
  133.      return *FH->fh_Ptr++;
  134.     }
  135.    else MySetIoErr (0L);
  136.  
  137.  return -1L;
  138. }
  139.  
  140. LONG __regargs FFRRead(struct FFRHandle *FH,APTR Buffer,LONG Bytes)
  141.  
  142. {
  143.  LONG Left;
  144.  UBYTE *Ptr;
  145.  
  146.  if (Bytes<=FH->fh_Left)
  147.   {
  148.    (void)memcpy(Buffer,FH->fh_Ptr,Bytes);
  149.    FH->fh_Ptr+=Bytes;
  150.    FH->fh_Left-=Bytes;
  151.  
  152.    return Bytes;
  153.   }
  154.  
  155.  Left=Bytes;
  156.  Ptr=Buffer;
  157.  while (Left>0L)
  158.   {
  159.    LONG Size;
  160.  
  161.    Size=__builtin_min(FH->fh_Left,Left);
  162.    if (Size)
  163.     {
  164.      (void)memcpy(Ptr,FH->fh_Ptr,Size);
  165.      FH->fh_Ptr+=Size;
  166.      FH->fh_Left-=Size;
  167.      Ptr+=Size;
  168.  
  169.      Left-=Size;
  170.     }
  171.  
  172.    if (Left)
  173.     if (!FH->fh_Flag) break;
  174.     else
  175.      if (!EndRead(FH)) return -1L;
  176.   }
  177.  
  178.  return Bytes-Left;
  179. }
  180.